package org.yn.base.util;


import com.alibaba.excel.util.StringUtils;
import org.yn.dept.domain.Shop;
import org.yn.emp.domain.Employee;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class LoginContext {
    public static Map<String,Object> loginMap = new HashMap<>();

    public static List<String> userAuth=new LinkedList<>();

    public static Employee getLoginUser(HttpServletRequest request){
        String token = request.getHeader("token");
//        if (StringUtils.isEmpty(token)){
//            throw new RuntimeException("未登录请重新登录");
//        }
        Object user = loginMap.get(token);
        if (Objects.nonNull(user)){
            return (Employee)user;
        }else{
            Employee employee = new Employee();
            employee.setId(1L);
            employee.setUsername("默认管理员");
            return employee;
        }

    }

    /**
     * 登录成功后调用
     * @param loginUser
     */
    public static void setLoginAdmin(Employee loginUser){
        //暂时不实现--要存放session,redis

    }

    /**
     * 获取登录用户--写死的
     * @return
     */
    public static Employee getLoginAdmin(){
        //从session或者redis中获取  现在模拟获取一个用户
        Employee employee = new Employee();
        employee.setId(340L);
        employee.setUsername("admin");
        Shop shop = new Shop();
        shop.setId(48L);
        employee.setShop(shop);
        return employee;
    }

    /**
     * 获取登录用户--写死的
     * @return
     */
    public static Employee getLoginAdmin(HttpServletRequest request){
        String token = request.getHeader("token");

        if (StringUtils.isBlank(token)){//token为空
            //从session或者redis中获取  现在模拟获取一个用户
            Employee employee = new Employee();
            employee.setId(340L);
            employee.setUsername("admin");
            Shop shop = new Shop();
            shop.setId(48L);
            shop.setName("姚桑成都温江官方旗舰店");
            employee.setShop(shop);
            return employee;
        }
        //不为空时
        Object obj = loginMap.get(token);

        if (Objects.isNull(obj)){

            Employee employee = new Employee();
            employee.setId(340L);
            employee.setUsername("admin");
            Shop shop = new Shop();
            shop.setId(48L);
            shop.setName("姚桑成都温江官方旗舰店");
            employee.setShop(shop);
            return employee;
        }

//        return (Employee) obj;
        //暂时先写死
        return getLoginAdmin();
    }
}
